package com.qianbao.qianbaobusiness.ui.cashier;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.Constant;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.DESedeEncrypt;
import com.google.gson.Gson;
import com.qianbao.qianbaobusiness.json.RequestBean;
import com.qianbao.qianbaobusiness.json.SettlementDetailBean;
import com.qianbao.qianbaobusiness.json.UnCheckedListBean;

import java.util.HashMap;

import javax.inject.Inject;

import timber.log.Timber;

public class SettlementPresenter extends RxMvpPresenter<SettlementView> {
    BossApi bossApi;

    @Inject
    public SettlementPresenter(BossApi api) {
        bossApi = api;
    }

    /**
     * 根据商户号查询待结算账单
     * @param merchantNos
     */
    public void queryDaysUnCheck(String merchantNos) {
        HashMap<String, String> merchantMap = new HashMap<>();

        merchantMap.put("mCode", merchantNos);
//        String desedeStr = "";
//        try {
//            desedeStr = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", new Gson()
//                    .toJson(merchantMap));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        HashMap<String, String> paraMap = new HashMap<>();
//        paraMap.put("context", desedeStr);
        bossApi.queryDaysUnCheck(Constant.encryptMap(merchantMap))
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<String>>() {
                    @Override
                    public void onNext(Data<String> stringData) {
                        if(RxObserver.checkJsonCode(stringData,false)){
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I"
                                        , stringData.getResult());
                                // new Gson().fromJson()
                                UnCheckedListBean bean = new Gson().fromJson(result
                                        ,UnCheckedListBean.class);
                                Timber.e("result = " + result);
                                getView().onSuccess(bean.get(0));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    /**
     * 根据商户号，起始时间分页查询
     * @param posMerchantNo
     * @param pageNum
     * @param s
     * @param startData
     * @param endData
     */
    public void queryColtCheckedPage(String posMerchantNo, int pageNum, int s
            , String startData, String endData) {
        RequestBean requestBean = new RequestBean(posMerchantNo,pageNum,s,startData,endData);
        String desedeStr = "";
        try {
            desedeStr = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", new Gson()
                    .toJson(requestBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String, String> paraMap = new HashMap<>();
        paraMap.put("context", desedeStr);
        bossApi.queryColtCheckedPage(paraMap)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<String>>(){
                    @Override
                    public void onComplete() {
                        getView().onComplete();
                    }

                    @Override
                    public void onNext(Data<String> stringData) {
                        if(RxObserver.checkJsonCode(stringData,false)){
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I"
                                        , stringData.getResult());
                                // new Gson().fromJson()
                                SettlementDetailBean bean = new Gson().fromJson(result
                                        ,SettlementDetailBean.class);
                                Timber.e("result = " + result);
                                getView().onPageSuccess(bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().getError(e);
                    }
                });
    }
}
